<template>
	<view class="u-wrap">
		<!-- 子分类返回按钮 -->
		<view v-if="isSonCategory" class="back-to-main" @click="backToMain">
			<u-icon name="arrow-left" color="#5C64FF" size="32"></u-icon>
			<text class="back-text">返回主分类</text>
		</view>
		
		<view class="p-t-25 p-b-25 p-l-40 p-r-40">
			<u-search placeholder="搜索服务" v-model="keyword" bg-color="#fff" height="80"></u-search>
		</view>
		<view class="u-menu-wrap">
			<scroll-view scroll-y scroll-with-animation class="u-tab-view menu-scroll-view" :scroll-top="scrollTop">
				<view v-for="(item,index) in classListData" :key="index" class="u-tab-item" 
					:class="[current==index ? 'u-tab-item-active' : '',(current + 1) == index ? 'u-tab-item-active-radius' : '']" 
				 :data-current="index" @tap.stop="swichMenu(index)">
					<text class="u-line-1">{{item.category_name}}</text>
				</view>
			</scroll-view>
			<block v-for="(item,index) in classListData" :key="index" >
				<scroll-view scroll-y class="right-box" v-if="current==index">
					<view class="page-view">
						<view>
							<image :src="item.category_image" class="w-450 h-280 m-b-32 br-20"></image>
						</view>
						<view class="class-item" v-for="(item1, index1) in item.two_list" :key="index1">
							<view class="item-title">
								<text>{{item1.category_name}}</text>
							</view>
							<view class="item-container">
								<!-- 主分类视图 -->
								<view v-if="!isSonCategory" class="thumb-box" 
									v-for="(item3,index3) in item1.three_list" :key="index3"
									@click="itemClick(item3)">
									<image class="item-menu-image " :src="item3.category_image"></image>
									<view class="item-menu-name">{{item3.category_name}}</view>
								</view>
								
								<!-- 子分类视图 -->
								<view v-if="isSonCategory" class="thumb-box" 
									v-for="(item3,index3) in item1.service_list" :key="index3"
									@click="sonItemClick(item3)">
									<image class="item-menu-image " :src="item3.service_image"></image>
									<view class="item-menu-name">{{item3.service_name}}</view>
								</view>
							</view>
						</view>
					</view>
				</scroll-view>
			</block>
		</view>
	</view>
</template>

<script>
	import classifyData from "./classify.data.js"
	export default {
		data() {
			return {
				tabbar: classifyData,
				scrollTop: 0, //tab标题的滚动条位置
				current: 0, // 预设当前项的值
				menuHeight: 0, // 左边菜单的高度
				menuItemHeight: 0, // 左边菜单item的高度
				keyword:"",
				classListData:[],
				isSonCategory: false // 标记是否处于子分类状态
			}
		},
		computed: {
			
		},
		onShow(){
			this.getList()
		},
		methods: {
			async getList(){
				const res = await this.$service({
					url:"/api/member.index/getCategoryList",
					method:"GET"
				})
				console.log(res)
				this.classListData = res.data
				this.isSonCategory = false; // 重置为主分类状态
			},
			itemClick(data){
				console.log(data)
				if(data.has_son == 0){
					this.$jump.navigateTo('/pages/shops/list?id=' + data.category_id + '&categoryName=' + encodeURIComponent(data.category_name))
				}else{
					this.current = 0;
					this.getSonCategoryList(data.category_id)
				}
			},
			
			// 子分类点击事件
			sonItemClick(data){
				console.log('子分类点击:', data)
				// 子分类直接跳转到商品详情页面，可以传递不同的参数
				this.$jump.navigateTo('/pages/shops/detail?id=' + data.service_id)
			},
			async getSonCategoryList(id){
				const res = await this.$service({
					url:"/api/member.index/getSonCategoryList",
					data:{
						category_id:id
					}
				})
				console.log(res)
				// 当调用 getSonCategoryList 时，将数据中的 three_list 改为 four_list
				this.classListData = res.data;
				this.isSonCategory = true; // 设置为子分类状态
			},
			
			// 返回主分类
			backToMain(){
				this.getList(); // 重新获取主分类数据
			},
			getImg() {
				return Math.floor(Math.random() * 35);
			},
			// 点击左边的栏目切换
			async swichMenu(index) {
				if(index == this.current) return ;
				this.current = index;
				// 如果为0，意味着尚未初始化
				if(this.menuHeight == 0 || this.menuItemHeight == 0) {
					await this.getElRect('menu-scroll-view', 'menuHeight');
					await this.getElRect('u-tab-item', 'menuItemHeight');
				}
				// 将菜单菜单活动item垂直居中
				this.scrollTop = index * this.menuItemHeight + this.menuItemHeight / 2 - this.menuHeight / 2;
			},
			// 获取一个目标元素的高度
			getElRect(elClass, dataVal) {
				new Promise((resolve, reject) => {
					const query = uni.createSelectorQuery().in(this);
					query.select('.' + elClass).fields({size: true},res => {
						// 如果节点尚未生成，res值为null，循环调用执行
						if(!res) {
							setTimeout(() => {
								this.getElRect(elClass);
							}, 10);
							return ;
						}
						this[dataVal] = res.height;
					}).exec();
				})
			}
		}
	}
</script>


<style>
	page{background: #F7F9FA;}
</style>
<style lang="scss" scoped>
	.u-wrap {
		height: calc(100vh);
		/* #ifdef H5 */
		height: calc(100vh - var(--window-top));
		/* #endif */
		display: flex;
		flex-direction: column;
	}

	.u-search-box {
		padding: 18rpx 30rpx;
	}

	.u-menu-wrap {
		flex: 1;
		display: flex;
		overflow: hidden;
	}


	.u-tab-view {
		width: 230rpx;
		height: 100%;
		background-color: #fff;
	}

	.u-tab-item {
		width: 230rpx;
		height: 110rpx;
		background: #ffffff;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: center;
		font-weight: 600;
		color:#333;
		line-height: 1;
		
	}
	.u-tab-item-active-radius{
		border-radius:0 60rpx 0 0 ;
	}
	.u-tab-item-active {
		position: relative;
		color: #5C64FF;
		background: #F7F9FA;
	}

	.u-tab-view {
		height: 100%;
	}
	
	.right-box {
		background-color: rgb(250, 250, 250);
		padding-bottom:20rpx;
	}
	
	.page-view {
		padding: 32rpx; 
	}
	
	.class-item {
		margin-bottom: 30rpx;
		padding: 16rpx;
		border-radius: 8rpx;
	}
	
	.item-title {
		font-size: 26rpx;
		color: $u-main-color;
		font-weight: bold;
	}
	
	.item-menu-name {
		font-weight: normal;
		font-size: 24rpx;
		color: #333;
		margin-top:8rpx;
	}
	
	.item-container {
		display: flex;
		flex-wrap: wrap;
	}
	
	.thumb-box {
		width: 33.333333%;
		display: flex;
		align-items: center;
		justify-content: center;
		flex-direction: column;
		margin-top: 20rpx;
	}
	
	.item-menu-image {
		width: 130rpx;
		height: 130rpx;
		border-radius: 25rpx;
	}
	
	
	.son-category-item .item-menu-image {
		border: 2rpx solid #5C64FF;
		opacity: 0.9;
	}
	
	.son-category-item .item-menu-name {
		color: #5C64FF;
		font-weight: 500;
	}
	
	/* 返回主分类按钮样式 */
	.back-to-main {
		display: flex;
		align-items: center;
		padding: 20rpx 40rpx;
		background: #ffffff;
		border-bottom: 1rpx solid #f0f0f0;
		box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
	}
	
	.back-text {
		margin-left: 15rpx;
		font-size: 28rpx;
		color: #5C64FF;
		font-weight: 500;
	}
	
</style>
